package com.doubao.doubao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.doubao.doubao.common.api.ApiResult;
import com.doubao.doubao.mapper.BmsCommentMapper;
import com.doubao.doubao.model.dto.CommentDTO;
import com.doubao.doubao.model.dto.ParentCommentDTO;
import com.doubao.doubao.model.entity.BmsComment;
import com.doubao.doubao.model.entity.UmsUser;
import com.doubao.doubao.model.vo.CommentVO;
import com.doubao.doubao.service.IBmsCommentService;
import com.doubao.doubao.service.IUmsUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.doubao.doubao.jwt.JwtUtil.USER_NAME;


@RestController
@RequestMapping("/comment")
public class BmsCommentController extends BaseController {

    @Resource
    private IBmsCommentService bmsCommentService;
    @Resource
    private IUmsUserService umsUserService;
    @Resource
    BmsCommentMapper bmsCommentMapper;

    @GetMapping("/get_comments")
    public ApiResult<List<CommentVO>> getCommentsByTopicID(@RequestParam(value = "topicid", defaultValue = "1") String topicid) {
        List<CommentVO> lstBmsComment = bmsCommentService.getCommentsByTopicID(topicid);
        return ApiResult.success(lstBmsComment);
    }

    @GetMapping("/get_ParentCom")
    public ApiResult<List<CommentVO>> getParentCom(@RequestParam(value = "parentid", defaultValue = "1") String parentid) {
        List<CommentVO> lstParentCom = bmsCommentService.getParentCom(parentid);
        return ApiResult.success(lstParentCom);
    }

    @GetMapping("/get_ParentCom2")
    public ApiResult<List<CommentVO>> getParentCom2(@RequestParam(value = "itemid", defaultValue = "1") String itemid) {
        List<CommentVO> lstParentCom2 = bmsCommentService.getParentCom2(itemid);
        return ApiResult.success(lstParentCom2);
    }


    @PostMapping("/add_comment")
    public ApiResult<BmsComment> add_comment(@RequestHeader(value = USER_NAME) String userName,
                                             @RequestBody CommentDTO dto) {
        UmsUser user = umsUserService.getUserByUsername(userName);
        BmsComment comment = bmsCommentService.create(dto, user);
        return ApiResult.success(comment);
    }

    @PostMapping("/add_ParentCom")
    public ApiResult<BmsComment> add_Parchment(@RequestHeader(value = USER_NAME) String userName,
                                               @RequestBody ParentCommentDTO dto) {
        UmsUser user = umsUserService.getUserByUsername(userName);
        BmsComment parchment = bmsCommentService.createParCom(dto, user);
        return ApiResult.success(parchment);
    }

    @DeleteMapping("/deleteComments/{id}")
    public ApiResult<?> delete(@PathVariable Long id){
        bmsCommentMapper.deleteById(id);
        return ApiResult.success();
    }

//    @DeleteMapping("/deletePCComments/{parentId}")
//    public ApiResult<?> delete(@PathVariable  String parentId){
//        bmsCommentMapper.deleteById(parentId);
//        return ApiResult.success();
//    }

//      public void deleteByMap(){
//        Map<String, Object> deleteMap = new HashMap<>();
//        deleteMap.put("name", "张1");
//        deleteMap.put("age", 33);
//        int rows = bmsCommentMapper.deleteByMap(deleteMap);
//        System.out.println("删除条数："+rows);
//    }

       @DeleteMapping("/deletePCComments/{parentId}")
        public ApiResult<BmsComment>  deleteWrapper(@PathVariable  String parentId){
        LambdaQueryWrapper<BmsComment> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(BmsComment::getParentId, parentId);
         bmsCommentMapper.delete(lambdaQuery);
         bmsCommentMapper.deleteById(parentId);
           return ApiResult.success();
    }


}
